package com.youxin.busfoundation.utils;

import org.springframework.http.ResponseEntity;

import com.youxin.busfoundation.bean.BusinessMessages;
import com.youxin.busfoundation.bean.wexin.WxOpenAccessTokenBean;
import com.youxin.busfoundation.bean.wexin.WxOpenUserInfoBean;
import com.youxin.busfoundation.bean.wexin.WxPublicTokenExpiresBean;
import com.youxin.busfoundation.bean.wexin.WxPublicUserInfoBean;
import com.youxin.busfoundation.common.BusinessException;
import com.youxin.busfoundation.net.WebVisitor;
import com.youxin.foundation.utils.JsonUtils;
import com.youxin.foundation.utils.StringUtils;

/**
 * 微信方法
 * @author yingde.cao
 *
 */
public class WexinUtils {
	/**
	 * 获取开放平台access_token
	 * https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
	 */	
	public static WxOpenAccessTokenBean getOpenAccessToken(String appId,String secret,String code) throws BusinessException
	{
		String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
		ResponseEntity<String> entity=WebVisitor.instance().httpGetEntity(url);		
		WxOpenAccessTokenBean bean=JsonUtils.stringToFasterxmlObject(entity.getBody(), WxOpenAccessTokenBean.class,true);
		if(bean!=null&&!StringUtils.isNullOrEmpty(bean.getAccessToken()))
		{
			return bean;
		}
		throw new BusinessException(BusinessMessages.WxOpenGetAccessTokenMessage.getCode(),entity.getBody()); 
	}
	
	/**
	 * 获取开放平台用户信息
	 * https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
	 */
	public static WxOpenUserInfoBean getOpenUserInfo(String accessToken,String openId)
	{
		String url="https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId;
		ResponseEntity<String> entity=WebVisitor.instance().httpGetEntity(url);		
		WxOpenUserInfoBean bean=JsonUtils.stringToFasterxmlObject(entity.getBody(), WxOpenUserInfoBean.class,true);
		if(bean!=null&&!StringUtils.isNullOrEmpty(bean.getOpenid()))
		{
			return bean;
		}
		throw new BusinessException(BusinessMessages.WxOpenGetUserInfoMessage.getCode(),entity.getBody()); 
	}
	
	/**
	 * 获取公共平台的access_token
	 * https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
	 */
	public static WxPublicTokenExpiresBean getPublicToken(String appId,String secret,String code)
	{
		String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
		ResponseEntity<String> entity=WebVisitor.instance().httpGetEntity(url);		
		WxPublicTokenExpiresBean bean=JsonUtils.stringToFasterxmlObject(entity.getBody(), WxPublicTokenExpiresBean.class,true);
		if(bean!=null&&!StringUtils.isNullOrEmpty(bean.getAccessToken()))
		{
			return bean;
		}
		throw new BusinessException(BusinessMessages.WxPublicGetAccessTokenMessage.getCode(),entity.getBody()); 
	}
	
	/**
	 * 获取公共平台用户信息
	 * https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN 
	 */
	public static WxPublicUserInfoBean getPublicUserInfo(String accessToken,String openId)
	{
		String url="https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
		ResponseEntity<String> entity=WebVisitor.instance().httpGetEntity(url);		
		WxPublicUserInfoBean bean=JsonUtils.stringToFasterxmlObject(entity.getBody(), WxPublicUserInfoBean.class,true);
		if(bean!=null&&!StringUtils.isNullOrEmpty(bean.getOpenid()))
		{
			return bean;
		}
		throw new BusinessException(BusinessMessages.WxPublicGetUserInfoMessage.getCode(),entity.getBody()); 
	}
	
}
